﻿<Window x:Class="MineSweeper.Windows.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mineSweeperWindows="clr-namespace:MineSweeper.Windows"
        xmlns:properties="clr-namespace:MineSweeper.Properties"
        xmlns:behaviors="clr-namespace:MineSweeper.Behaviors"
        xmlns:commands="clr-namespace:MineSweeper.Commands"
        xmlns:data="clr-namespace:MineSweeper.Data"
        xmlns:input="clr-namespace:System.Windows.Input;assembly=PresentationCore"
        xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
        xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions" 
        Name="mainWindow" 
        Title="Mine Sweeper" MinHeight="350" 
        WindowStartupLocation="CenterScreen"
        Height="{Binding Source={x:Static properties:Settings.Default}, Path=MainWindowHeight, Mode=TwoWay}"
        Width="{Binding Source={x:Static properties:Settings.Default}, Path=MainWindowWidth, Mode=TwoWay}"
        Loaded="mainWindow_Loaded"
        Icon="/MineSweeper;component/Resources/mine.png" DataContext="{DynamicResource tileMap}">
    <Window.Resources>
        <ObjectDataProvider ObjectType="{x:Type data:TileMap}" 
                                MethodName="Create" x:Key="tileMap"/>
    </Window.Resources>
    <Window.Style>
        <Style TargetType="Window" BasedOn="{StaticResource windowBackgroundStyle}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Source={x:Static properties:Settings.Default}, Path=GameOptionDifficultyInt}" Value="2">
                    <Setter Property="behaviors:WindowBehavior.AspectRatio" Value=" 1.5596351684351384"/>
                </DataTrigger>
            </Style.Triggers>
            <Setter Property="behaviors:WindowBehavior.AspectRatio" Value=".85"/>
        </Style>
    </Window.Style>
    <Window.InputBindings>
        <KeyBinding Gesture="F1" Command="{x:Static commands:GameCommands.ShowWindowModal}"
                          CommandParameter="{x:Type mineSweeperWindows:AboutBox}"/>
        <KeyBinding Gesture="F2" Command="{x:Static commands:GameCommands.NewGame}" CommandParameter="{Binding}"/>
        <KeyBinding Gesture="F4" Command="{x:Static commands:GameCommands.ShowWindowModal}"
                          CommandParameter="{x:Type mineSweeperWindows:Statistics}"/>
        <KeyBinding Gesture="F5" Command="{x:Static commands:GameCommands.ShowWindowModal}"
                          CommandParameter="{x:Type mineSweeperWindows:Options}"/>
        <KeyBinding Gesture="F7" Command="{x:Static commands:GameCommands.ShowWindowModal}"
                          CommandParameter="{x:Type mineSweeperWindows:ChangeAppearance}"/>
        <KeyBinding Gesture="Space" Command="{x:Static commands:GameCommands.UncoverTile}"
                          CommandParameter="{Binding FocusedTile}"/>
        <KeyBinding Modifiers="Shift" Key="Space" Command="{x:Static commands:GameCommands.ToggleTileMarker}"
                    CommandParameter="{Binding FocusedTile}"/>
        <KeyBinding Gesture="Up" Command="{x:Static commands:GameCommands.FocusMoveUp}"
                          CommandParameter="{Binding}"/>
        <KeyBinding Gesture="Down" Command="{x:Static commands:GameCommands.FocusMoveDown}"
                          CommandParameter="{Binding}"/>
        <KeyBinding Gesture="Left" Command="{x:Static commands:GameCommands.FocusMoveLeft}"
                          CommandParameter="{Binding}"/>
        <KeyBinding Gesture="Right" Command="{x:Static commands:GameCommands.FocusMoveRight}"
                          CommandParameter="{Binding}"/>
    </Window.InputBindings>
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Closed">
            <ei:CallMethodAction TargetObject="{Binding Source={x:Static properties:Settings.Default}}" MethodName="Save"/>
        </i:EventTrigger>
        <i:EventTrigger EventName="Loaded">
            <ei:PlaySoundAction Source="Resources\Sounds\GameStart.wma"  />
        </i:EventTrigger>
    </i:Interaction.Triggers>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Menu>
            <MenuItem Header="_Game">
                <MenuItem Header="_New Game" InputGestureText="F2" Command="{x:Static commands:GameCommands.NewGame}" CommandParameter="{Binding}"/>
                <Separator/>
                <MenuItem Header="_Statistics" InputGestureText="F4"
                          Command="{x:Static commands:GameCommands.ShowWindowModal}"
                          CommandParameter="{x:Type mineSweeperWindows:Statistics}"/>
                <MenuItem Header="_Options" InputGestureText="F5"
                          Command="{x:Static commands:GameCommands.ShowWindowModal}"
                          CommandParameter="{x:Type mineSweeperWindows:Options}"/>
                <MenuItem Header="_Change Appearance" InputGestureText="F7"
                          Command="{x:Static commands:GameCommands.ShowWindowModal}"
                          CommandParameter="{x:Type mineSweeperWindows:ChangeAppearance}"/>
                <Separator/>
                <MenuItem Header="E_xit">
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="Click">
                            <ei:CallMethodAction TargetObject="{Binding ElementName=mainWindow}" MethodName="Close"/>
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                </MenuItem>
            </MenuItem>
            <MenuItem Header="_Help">
                <MenuItem Header="_View Help" InputGestureText="F1"/>
                <Separator/>
                <MenuItem Header="_About Mine Sweeper" 
                          Command="{x:Static commands:GameCommands.ShowWindowModal}"
                          CommandParameter="{x:Type mineSweeperWindows:AboutBox}"/>
            </MenuItem>
        </Menu>
        <Viewbox StretchDirection="Both" Grid.Row="1">
            <DockPanel LastChildFill="True">
                <Border DockPanel.Dock="Top" Height="30"/>
                <Border DockPanel.Dock="Bottom" Height="30">
                    <Grid HorizontalAlignment="Center">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="auto" />
                            <ColumnDefinition Width="auto" />
                            <ColumnDefinition Width="40" />
                            <ColumnDefinition Width="auto" />
                            <ColumnDefinition Width="auto" />
                        </Grid.ColumnDefinitions>
                        <Image Height="16" Source="/MineSweeper;component/Resources/timer.png" />
                        <Grid Width="40" Grid.Column="1">
                            <TextBlock Text="{Binding Seconds}" VerticalAlignment="Center" HorizontalAlignment="Center" Foreground="Black" />
                        </Grid>
                        <Grid Width="40" Grid.Column="3">
                            <TextBlock Text="{Binding PerceivedRemainingMines}" VerticalAlignment="Center" HorizontalAlignment="Center"/>
                        </Grid>
                        <Image Height="16" Grid.Column="4">
                            <Image.Style>
                                <Style TargetType="Image" >
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding Source={x:Static properties:Settings.Default}, Path=ActiveIconName}" Value="mineSweeper">
                                            <Setter Property="Source" Value="/MineSweeper;component/Resources/mine.png"/>
                                        </DataTrigger>
                                    </Style.Triggers>
                                    <Setter Property="Source" Value="/MineSweeper;component/Resources/flowergarden_gamethumb.png"/>
                                </Style>
                            </Image.Style>
                        </Image>
                    </Grid>
                </Border>
                <Border DockPanel.Dock="Left" Width="28"/>
                <Border DockPanel.Dock="Right" Width="28"/>
                <Border HorizontalAlignment="Stretch">
                    <ItemsControl Name="gameBoard" Width="{Binding TileMapActualWidth}" Height="{Binding TileMapActualHeight}"
                               ItemsSource="{Binding Tiles}" BorderBrush="DarkGray" BorderThickness="1">
                        <ItemsControl.ItemsPanel>
                            <ItemsPanelTemplate>
                                <UniformGrid Rows="{Binding Height}" Columns="{Binding Width}"/>
                            </ItemsPanelTemplate>
                        </ItemsControl.ItemsPanel>
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <ContentControl VerticalContentAlignment="Center" Height="18" Width="18" Style="{DynamicResource tileControlStyle}">
                                    <ContentControl.InputBindings>
                                        <MouseBinding MouseAction="RightClick" 
                                                      Command="{x:Static commands:GameCommands.ToggleTileMarker}"
                                                      CommandParameter="{Binding}"/>
                                    </ContentControl.InputBindings>
                                    <i:Interaction.Triggers>
                                        <i:EventTrigger EventName="MouseLeftButtonUp">
                                            <i:InvokeCommandAction 
                                                Command="{x:Static commands:GameCommands.UncoverTile}"
                                                CommandParameter="{Binding}"/>
                                        </i:EventTrigger>
                                        <i:EventTrigger EventName="MouseEnter">
                                            <i:InvokeCommandAction 
                                                Command="{x:Static commands:GameCommands.FocusTile}"
                                                CommandParameter="{Binding}"/>
                                        </i:EventTrigger>
                                        <i:EventTrigger EventName="MouseLeave">
                                            <i:InvokeCommandAction 
                                                Command="{x:Static commands:GameCommands.ClearFocusTile}"
                                                CommandParameter="{Binding}"/>
                                        </i:EventTrigger>
                                    </i:Interaction.Triggers>
                                    <ContentControl.Content>
                                        <Grid>
                                            <TextBlock FontSize="18" FontWeight="Bold" VerticalAlignment="Center" Margin="0,0,0,1">
                                                <TextBlock.Style>
                                                    <Style TargetType="TextBlock">
                                                        <Style.Triggers>                
                                                            <DataTrigger Binding="{Binding NearbyMineCount}" Value="0">
                                                                <Setter Property="Text" Value="{x:Null}"/>
                                                            </DataTrigger>
                                                            <DataTrigger Binding="{Binding State}" Value="Covered">
                                                                <Setter Property="Text" Value="{x:Null}"/>
                                                            </DataTrigger>
                                                            <DataTrigger Binding="{Binding State}" Value="Flagged">
                                                                <Setter Property="Text" Value="{x:Null}"/>
                                                            </DataTrigger>
                                                            <DataTrigger Binding="{Binding Type}" Value="Bomb">
                                                                <Setter Property="Text" Value="{x:Null}"/>
                                                            </DataTrigger>
                                                            <DataTrigger Binding="{Binding NearbyMineCount}" Value="1">
                                                                <Setter Property="Foreground" Value="Blue"/>
                                                            </DataTrigger>
                                                            <DataTrigger Binding="{Binding NearbyMineCount}" Value="2">
                                                                <Setter Property="Foreground" Value="Green"/>
                                                            </DataTrigger>
                                                            <DataTrigger Binding="{Binding NearbyMineCount}" Value="3">
                                                                <Setter Property="Foreground" Value="Red"/>
                                                            </DataTrigger>
                                                            <DataTrigger Binding="{Binding NearbyMineCount}" Value="4">
                                                                <Setter Property="Foreground" Value="Navy"/>
                                                            </DataTrigger>
                                                            <DataTrigger Binding="{Binding NearbyMineCount}" Value="5">
                                                                <Setter Property="Foreground" Value="DarkRed"/>
                                                            </DataTrigger>
                                                            <DataTrigger Binding="{Binding NearbyMineCount}" Value="6">
                                                                <Setter Property="Foreground" Value="DarkCyan"/>
                                                            </DataTrigger>
                                                            <DataTrigger Binding="{Binding NearbyMineCount}" Value="7">
                                                                <Setter Property="Foreground" Value="Red"/>
                                                            </DataTrigger>
                                                            <DataTrigger Binding="{Binding NearbyMineCount}" Value="8">
                                                                <Setter Property="Foreground" Value="DarkRed"/>
                                                            </DataTrigger>
                                                            <DataTrigger Binding="{Binding State}" Value="Question">
                                                                <Setter Property="Text" Value="?"/>
                                                                <Setter Property="Foreground" Value="White"/>
                                                            </DataTrigger>
                                                        </Style.Triggers>
                                                        <Setter Property="Text" Value="{Binding NearbyMineCount}"/>
                                                    </Style>
                                                </TextBlock.Style>
                                            </TextBlock>
                                            <Image Margin="2">
                                                <Image.Style>
                                                    <Style TargetType="Image">
                                                        <Style.Triggers>
                                                            <MultiDataTrigger>
                                                                <MultiDataTrigger.Conditions>
                                                                    <Condition Binding="{Binding State}" Value="Uncovered"/>
                                                                    <Condition Binding="{Binding Type}" Value="Bomb"/>
                                                                    <Condition Binding="{Binding Source={x:Static properties:Settings.Default}, Path=ActiveIconName}" Value="mineSweeper"/>
                                                                </MultiDataTrigger.Conditions>
                                                                <Setter Property="Source" Value="/MineSweeper;component/Resources/mine.png"/>
                                                            </MultiDataTrigger>
                                                            <MultiDataTrigger>
                                                                <MultiDataTrigger.Conditions>
                                                                    <Condition Binding="{Binding State}" Value="Uncovered"/>
                                                                    <Condition Binding="{Binding Type}" Value="Bomb"/>
                                                                    <Condition Binding="{Binding Source={x:Static properties:Settings.Default}, Path=ActiveIconName}" Value="flowerGarden"/>
                                                                </MultiDataTrigger.Conditions>
                                                                <Setter Property="Source" Value="/MineSweeper;component/Resources/flowergarden_gamethumb.png"/>
                                                            </MultiDataTrigger>
                                                            <DataTrigger Binding="{Binding State}" Value="Flagged">
                                                                <Setter Property="Source" Value="/MineSweeper;component/Resources/flag.png"/>
                                                            </DataTrigger>
                                                        </Style.Triggers>
                                                    </Style>
                                                </Image.Style>
                                            </Image>
                                        </Grid>
                                    </ContentControl.Content>
                                </ContentControl>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>
                </Border>
            </DockPanel>
        </Viewbox>
    </Grid>
</Window>
